// pages/index/law_service/leave_messages/leave_messages.js
import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from  "../../../../miniprogram_npm/@vant/weapp/dialog/dialog"

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    user_id: "",
    list:[],
    isLoading: true,
    isRefresh: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
      user_id: app.globalData.userInfo.applet_user_id
    })
  },

  /**
   * 下拉刷新
   * @param e
   */
  onRefresh: function (e) {
    this.setData({
      isRefresh: true
    })
    this.getLeaveMessages()
  },

  /**
   * 获取留言记录
   */
  getLeaveMessages: function (){
    this.setData({
      isLoading: true
    })
    http.get("information_reply/getSelect",{
      post_id: this.data.id
    }).then(res => {
      if (res.data.code === http.REQUEST_OK){
        this.setData({
          list: res.data.data
        })
      }
      this.setData({
        isRefresh: false,
        isLoading: false
      })
    }, err => {
      this.setData({
        isRefresh: false,
        isLoading: false
      })
    })
  },

  /**
   * 删除留言
   * @param e
   */
  onLeaveMessagesDelete: function (e) {
    const id = e.currentTarget.dataset.id
    Dialog.confirm({
      title: "温馨提示",
      message: "确定要删除该留言吗？"
    }).then(() => {
      Toast.loading({
        duration: 0,
        message: "删除中..."
      })
      http.putJson("information_reply/del",{
        id: id
      }).then(res => {
        if (res.data.code === http.REQUEST_OK){
          Toast.success("删除成功")
          this.getLeaveMessages()
        }else {
          Toast.fail(res.data.msg)
        }
      }, err => {
        Toast.fail("网络异常")
      })
    }).catch(() => {
      // on cancel
    })
  },

  /**
   * 我要留言
   */
  onAddLeaveMessages: function (){
    wx.navigateTo({
      url: "./add/add?id=" + this.data.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getLeaveMessages()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})